﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Data.Schema.Sql.SchemaModel;

namespace TFDP.Common
{
    public static class ConstraintExtensions
    {
        /// <summary>
        /// Check if a column in the constraint is nullable.
        /// If this is the case, a OUTER JOIN should be generated
        /// instead of a INNER JOIN
        /// </summary>
        /// <param name="constraint">The current instance</param>
        /// <returns>true if any of the columns in the constraint is nullable, otherwise false</returns>
        public static bool IsOuterJoin(this ISqlForeignKeyConstraint constraint)
        {
            var isOuterJoin = from col in constraint.Columns
                              where (col is ISqlSimpleColumn &&
                                    ((ISqlSimpleColumn)col).IsNullable)
                              select col;

            return (isOuterJoin.Count() > 0);
        }
    }
}
